package iammry.com.popuwindow;
import android.app.Dialog;
import android.content.Context;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ScrollView;
import java.util.ArrayList;
import java.util.List;
import iammry.com.R;
import iammry.com.interfaceData.ITCloseCallBack;

public class DialogWindows {
	private Context context;
	private Dialog dialog;
	private LinearLayout lLayout_content;
	private ScrollView sLayout_content;
	private List<SheetItem> sheetItemList;
	private Display display;
	LinearLayout iv_close;
	ITCloseCallBack callBack;

	public DialogWindows(Context context) {
		this.context = context;
		WindowManager windowManager = (WindowManager) context
				.getSystemService(Context.WINDOW_SERVICE);
		display = windowManager.getDefaultDisplay();
	}

	public DialogWindows(Context context, ITCloseCallBack callBack) {
		this.context = context;
		this.callBack=callBack;

		WindowManager windowManager = (WindowManager) context
				.getSystemService(Context.WINDOW_SERVICE);
		display = windowManager.getDefaultDisplay();
	}

	public DialogWindows builder() {
		// ��ȡDialog����
		View view = LayoutInflater.from(context).inflate(
				R.layout.dialog_layout, null);

		view.setMinimumWidth(display.getWidth());

		sLayout_content = (ScrollView) view.findViewById(R.id.sLayout_content);
		lLayout_content = (LinearLayout) view
				.findViewById(R.id.lLayout_content);
		iv_close = (LinearLayout) view.findViewById(R.id.iv_close);
		iv_close.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {


				dialog.dismiss();
				if(callBack!=null)
				{
					callBack.closeListening(true);
				}


			}
		});

		// ����Dialog���ֺͲ���
		dialog = new Dialog(context, R.style.ActionSheetDialogStyle);
		dialog.setContentView(view);
		Window dialogWindow = dialog.getWindow();
		dialogWindow.setGravity(Gravity.LEFT | Gravity.BOTTOM);
		WindowManager.LayoutParams lp = dialogWindow.getAttributes();
		lp.x = 0;
		lp.y = 0;
		dialogWindow.setAttributes(lp);
		return this;
	}

	public DialogWindows setCancelable(boolean cancel) {
		dialog.setCancelable(cancel);
		return this;
	}

	public DialogWindows setCanceledOnTouchOutside(boolean cancel) {
		dialog.setCanceledOnTouchOutside(cancel);
		return this;
	}

	/**
	 * 
	 * @param strItem
	 *            ��Ŀ����
	 * @param color
	 *            ��Ŀ������ɫ������null��Ĭ����ɫ
	 * @param listener
	 * @return
	 */
	public DialogWindows addSheetItem(String strItem, SheetItemColor color,
			OnSheetItemClickListener listener) {
		if (sheetItemList == null) {
			sheetItemList = new ArrayList<SheetItem>();
		}
		sheetItemList.add(new SheetItem(strItem, color, listener));
		return this;
	}

	/** set 布局 */
	public void setSheetItems(View view) {
		
		LayoutParams params = (LayoutParams) sLayout_content
				.getLayoutParams();
		params.height = display.getHeight() / 3 * 2;
		sLayout_content.setLayoutParams(params);
		
		lLayout_content.addView(view);

	}

	public void show() {
		//setSheetItems();
		dialog.show();
	}

	public interface OnSheetItemClickListener {
		void onClick(int which);
	}

	public class SheetItem {
		String name;
		OnSheetItemClickListener itemClickListener;
		SheetItemColor color;

		public SheetItem(String name, SheetItemColor color,
				OnSheetItemClickListener itemClickListener) {
			this.name = name;
			this.color = color;
			this.itemClickListener = itemClickListener;
		}
	}

	public enum SheetItemColor {
		Blue("#037BFF"), Red("#FD4A2E");

		private String name;

		private SheetItemColor(String name) {
			this.name = name;
		}

		public String getName() {
			return name;
		}

		public void setName(String name) {
			this.name = name;
		}
	}


}
